package com.jy.project.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.jy.project.dto.*;
import com.jy.project.ex.BizException;
import com.jy.project.service.MaterialService;
import com.jy.project.util.ServiceResultUtils;
import com.jy.project.vo.MaterialVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/v1")
@CrossOrigin
@Slf4j
public class MatericalController {

    @Autowired
    private MaterialService materialService;



    @Value("${login_url}")
    private  String LOGIN_URL;


    /**
     * 新增物资信息
     * */
    @RequestMapping("/material/dept-admin/insertMaterialDTO")
    @ResponseBody
    public Object insertMaterialDTO(@Validated MaterialDTO dto){
        materialService.insertMaterialDTO(dto);
        return ServiceResultUtils.success(null);
    }


    /**
     * 查看空闲状态的物资
     * */
    @RequestMapping("/material/dept-admin/listNotUsedMaterialVO")
    @ResponseBody
    public Object listNotUsedMaterialVO(@Validated  MaterialQueryDTO dto,Long userId){
        dto.setManagementUserId(userId);
        List<MaterialVO> materialVOS = materialService.listNotUsedMaterialVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(materialVOS));
    }

    /**
     * 查看已经在使用状态的物资
     * */
    @RequestMapping("/material/dept-admin/listUsedMaterialVO")
    @ResponseBody
    public Object usedListMaterialVO(@Validated  MaterialQueryDTO dto,Long userId){
        dto.setManagementUseUserId(userId);
        List<MaterialVO> materialVOS = materialService.listUsedMaterialVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(materialVOS));
    }

    /**
     * 查看当前使用人所有物资
     * */
    @RequestMapping("/material/dept-admin/listUserMaterialVO")
    @ResponseBody
    public Object listUserMaterialVO(@Validated  MaterialQueryDTO dto){

        List<MaterialVO> materialVOS = materialService.listUserMaterialVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(materialVOS));
    }


    /**
     * 查看所有物资
     * */
    @RequestMapping("/material/dept-admin/listMaterialVO")
    @ResponseBody
    public Object listMaterialVO(@Validated  MaterialQueryDTO dto,Long userId){
        dto.setManagementUserId(userId);
        List<MaterialVO> materialVOS = materialService.listMaterialVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(materialVOS));
    }
    /**
     * 查看所有物资
     * */
    @RequestMapping("/material/dept-admin/listMyMaterialVO")
    @ResponseBody
    public Object listMyMaterialVO(@Validated  MaterialQueryDTO dto,Long userId){
        dto.setUseUserId(userId);
        List<MaterialVO> materialVOS = materialService.listMaterialVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(materialVOS));
    }


    /**
     * 根据资产编号查询
     * */
    @RequestMapping("/material/getMaterialVO/{materialNo}")
    @ResponseBody
    public Object getMaterialVO(@PathVariable("materialNo")String materialNo){
        MaterialVO materialVO = materialService.getMaterialVO(materialNo);
        return ServiceResultUtils.success(materialVO);
    }
    /*
     *资产复制
     * */
    @RequestMapping("/material/dept-admin/copy")
    @ResponseBody
    public Object copy(@RequestHeader("token") String token, MatericalCopyDTO dto){
        materialService.copy(dto);
        return ServiceResultUtils.success(null);
    }


    /*
     *资产修改
     * */
    @RequestMapping("/material/dept-admin/update")
    @ResponseBody
    public Object update(@RequestHeader("token") String token, MaterialUpdateDTO material){
        materialService.update(material);
        return ServiceResultUtils.success(null);
    }

    /*
     *资产删除
     * */
    @RequestMapping("/material/dept-admin/delete")
    @ResponseBody
    public Object delete(@RequestBody String[] materialNos){
        materialService.deletes(materialNos);
        return ServiceResultUtils.success(null);
    }

    /*
     *扫码接口
     * */
    @RequestMapping("/material/scan/{materialNo}")
    public void scan(HttpServletRequest request,HttpServletResponse response, @PathVariable("materialNo")String materialNo) throws IOException {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            response.sendRedirect(LOGIN_URL);
            return;
        }
        response.setCharacterEncoding("UTF-8");
        //设置l浏览器接受内容时所使用的编码方式
        response.setContentType("text/html;charset = UTF-8");
        List<MaterialVO> materialVOS = materialService.listMaterialVOByMaterialNos(materialNo);
        response.getWriter().write(JSON.toJSONString(ServiceResultUtils.success(materialVOS)));
    }

    /**
     * 根据申请编号获取资产详情
     * */
    @RequestMapping("/material/getMaterialByNo")
    @ResponseBody
    public Object getMaterialByNo(@RequestBody ApplyDTO queryDTO){
        String applyNo = queryDTO.getApplyNo();
        if (StringUtils.isEmpty(applyNo)){
            throw new BizException("申请单号不能为空");
        }
        MaterialVO materialVO = materialService.getMaterialVO(queryDTO);
        return ServiceResultUtils.success(materialVO);
    }



}
